#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll mod;
ll C[5005][5005];

// 预处理组合数， 杨辉三角
void init() {
  for (int i = 0; i <= 5000; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j < i; j++)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
  }
}

int a[300005];

void solve() {
  int T;
  cin >> T >> mod;
  init();
  while (T--) {
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      sum += a[i];
    }
    ll ans = 1;
    for (int i = 1; i <= n; i++) {
      ans = ans * C[sum][a[i]] % mod;
      sum -= a[i];
    }
    cout << ans << "\n";
  }
}